package com.reds.backend.service.impl.admin;

import cn.dev33.satoken.secure.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.reds.backend.pojo.Admin;
import com.reds.backend.service.admin.adminHandleService;
import com.reds.backend.utils.adminUtils.admin.StpAdminUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.reds.backend.mapper.adminMapper;
import com.reds.backend.service.admin.emailSendService;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class adminHandleServiceImpl implements adminHandleService {
    @Autowired
    adminMapper adminMapper;
    @Autowired
    emailSendService emailSendService;

//    @Autowired  什么意思，看不懂
//    public AdminServiceImpl(AdminMapper adminMapper) {
//        this.adminMapper = adminMapper;
//    }

    String codeCheck;
    @Override
    public String sendAdminEmail(String name, String email) {

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        QueryWrapper<Admin> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("email",email);
        Admin admin = adminMapper.selectOne(queryWrapper);
        Admin admin1 = adminMapper.selectOne(queryWrapper1);
        if( admin !=null)
            return "管理员名已注册";
        else if(admin1 != null)
            return "邮箱已注册";
        else{
            codeCheck = emailSendService.sendRegisterEmail("2631854038@qq.com",
                    email,
                    "2631854038@qq.com",
                    "江西省红色文化资源管理系统验证码",
                    "");
            return "success";
        }
    }

    @Override
    public String codeCheck(String name,String password,String email,String code) {
        if(!code.equalsIgnoreCase(codeCheck))
            return "验证码错误";
        Admin admin = new Admin();
        admin.setEmail(email);
        admin.setName(name);
        admin.setPassword(BCrypt.hashpw(password));
        Date date = new Date();
        admin.setTime(date);
        admin.setRole("管理员");
        adminMapper.insert(admin);
        return "success";
    }


    @Override
    public Map<String,String> loginAdmin(String email, String password) {//
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        Admin admin = adminMapper.selectOne(queryWrapper);
        Map<String,String> map = new HashMap<>();
        if(admin == null) map.put("message","邮箱不存在");
        else if(!BCrypt.checkpw(password,admin.getPassword()))
        {
            map.put("message","密码错误");
            return map;
        }
        else {
            if(admin.getRole().equals("待审核")){
                map.put("message","账号审核中");
                return map;
            }else {
                StpAdminUtil.login(admin.getId());
                map.put("message","success");
                map.put("token",StpAdminUtil.getTokenValue());
                return map;
            }
        }
        return map;
    }
}